#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<int> findOrder(int n, vector<vector<int>>& p) {
        unordered_map<int, vector<int>>hashi;
        vector<int>in(n);
        for (auto e : p)
        {
            int a = e[0], b = e[1];
            hashi[b].push_back(a);
            in[a]++;
        }

        queue<int>q;
        vector<int>lesson;
        for (int i = 0; i < n; i++)
        {
            if (in[i] == 0) q.push(i);
        }
        while (q.size())
        {
            int t = q.front(); q.pop();
            lesson.push_back(t);
            for (auto x : hashi[t])
            {
                in[x]--;
                if (in[x] == 0) q.push(x);
            }

        }
        for (auto e : in)
        {
            if (e) return {};
        }
        return lesson;
    }
};